const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    inputShowed: false,
    inputVal: "",//search value
    products: [],
    schoolList: [],
    param: {},
    page: 1,
    endFlag: false,
    loadingFlag: false,
    regions: [],
    activeRegionId: 0,
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    regionId: 0,
    starRating: 0,
    showType: 3,
    totalPages: 1
  },
  onLoad: function (cate) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        //设置scroll-view 的高度
        var scollHeight = res.screenHeight - 90 - 65;
        that.setData({
          scollHeight: scollHeight,
          schoolList: []
        }
        );
        that.getRegions();
        that.getSchools();
      }
    })
  },

  getRegions: function () {
    var that = this;
    util.request(api.RegionList).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          regions: res.data.list
        });
      }

    });
  },

  changeRegion: function (e) {
    this.setData({
      regionId: e.currentTarget.id
    });
    this.getSchools();
  },

  getSchools: function () {

    var that = this;

    util.request(api.SchoolList, {
      regionId: that.data.regionId,
      starRating: that.data.starRating,
      type: that.data.showType,
      // page: that.data.page,
      // limit: that.data.limit
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          schoolList: res.data.list,
          totalPages: res.data.pages
        });
      }

    });

  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getSchools();
    } else {
      // wx.showToast({
      //   title: '没有更多数据了',
      //   icon: 'none',
      //   duration: 2000
      // });
      this.setData({
        endFlag: true
      });
      return false;
    }
  },


  //事件处理函数
  bindViewTap: function (e) {
    var param = e.currentTarget.dataset;
    var id = param["id"];
    var name = param["name"]

    // 存储数据
    let pagetotal = getCurrentPages() //所有的路径 数组
    let parpage = pagetotal[pagetotal.length - 2] //父级页面

    parpage.setData({
      jiaxiaoId: id,
      jiaxiao: name,
      classTypeId: 0,
      classType: '请选择班型',
      price: 0
    })
    wx.navigateBack({
      delta: 1
    });

  }
})